<?php
class totalOutStock extends Admin {

    private $tab = array( 'inStock' => '入库管理','inGoodsEit'=>'货品管理');
    private $color = array('深红', '大红','橘红','橘黄 ',
        '玫瑰红','橙色','深黄','淡黄','草绿',
        '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
        '天蓝', '湖蓝', '黑色', '白色', '紫色',
        '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
        "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");

    public function __construct() {
        parent::__construct();
        $this->pk = "inOutStockNo";
    }

    public function index( $c, $a ) {

        include $this->display( $c );
    }

    public  function preViewTotalStock()
    {
       // $sql = " CREATE TEMPORARY TABLE tmp_outStock select B.amount,B.goodsNo,B.goodsName,B.orderNo,B.color,B.size from  xbl_order as B,xbl_employeeOrder as A where A.orderNo = B.orderNo and A.status = 0 ";   //不是admin用户需要加上用户ID的判断。

       // $sql = " select B.amount as postNumber,B.goodsNo,B.goodsName,B.orderNo,B.color,B.size ,C.goodsNumber,(C.goodsNumber - B.amount) as resNumber from  xbl_order as B,xbl_employeeOrder as A ,xbl_stockInfo as C where A.orderNo = B.orderNo and A.status = 0 group by  goodsNo,color,size";   //不是admin用户需要加上用户ID的判断。
       $sql = "DROP TEMPORARY TABLE IF EXISTS tmp_outStock; ";
       $this->exec($sql);

       $sql = "CREATE TEMPORARY TABLE tmp_outStock (SELECT SUM(amount)AS postNumber,goodsNo,color,size FROM xbl_order  WHERE orderNo IN (SELECT orderNo FROM xbl_employeeorder WHERE STATUS=0";
        if($this->admin['rights']  != 0 )
            $sql = $sql." and customId = ".$this->admin['id'];

        $sql = $sql.") GROUP BY goodsNo,color,size); ";
        $this->exec($sql);

       $sql =" select A.*,B.goodsNumber,(B.goodsNumber - A.postNumber) AS resNumber FROM tmp_outStock AS A,xbl_stockInfo AS B WHERE A.goodsNo = B.goodsNo AND A.color = B.color AND A.size = B.size;";

        //$sql = "select * from xbl_stockInfo";
        print_r($sql);

        $result = $this->Paged()->findSql($sql);

        print_r($result);


        $pager = $result['page'];

        $result = $result['result'];

       foreach($result as $key=>$val)
        {
            $result[$key]['color'] = $this->color[$val['color']];
            $result[$key]['size'] = $this->size[$val['size']];
        }

        return $result;

    }


    public  function postTotalStock()
    {

        $sql = "DROP TEMPORARY TABLE IF EXISTS tmp_outStock; ";
        $this->exec($sql);

        $sql = "CREATE TEMPORARY TABLE tmp_outStock (SELECT SUM(amount)AS postNumber,goodsNo,color,size FROM xbl_order  WHERE orderNo IN (SELECT orderNo FROM xbl_employeeorder WHERE STATUS=0";
        if($this->admin['rights']  != 0 )
            $sql = $sql." and customId = ".$this->admin['id'];

        $sql = $sql.") GROUP BY goodsNo,color,size); ";
        $this->exec($sql);

        $sql =" select A.*,B.goodsNumber,(B.goodsNumber - A.postNumber) AS resNumber FROM tmp_outStock AS A,xbl_stockInfo AS B WHERE A.goodsNo = B.goodsNo AND A.color = B.color AND A.size = B.size;";

        //$sql = "select * from xbl_stockInfo";
        print_r($sql);

        $result = $this->Paged()->findSql($sql);

       print_r($result);


        $pager = $result['page'];

        $result = $result['result'];

        $judgeVar = 0;

        foreach($result as $key=>$val)
        {
            if($val['resNumber'] < 0 )
            {
                $judgeVar = 1;
                break;
            }
        }

        if(0 == $judgeVar)
        {
            /***更新库存***/
            $sql = " UPDATE xbl_stockInfo as C ,tmp_outStock as B SET C.goodsNumber = C.goodsNumber - B.postNumber where   C.goodsNo = B.goodsNo AND C.color =B.color AND C.size = B.size";   //不是admin用户需要加上用户ID的判断。
            $this->exec($sql);

            /**添加出库单***/
            $sql = " select B.orderNo from  xbl_order as B,xbl_employeeOrder as A ,xbl_stockInfo as C where A.orderNo = B.orderNo and A.status = 0 ";   //不是admin用户需要加上用户ID的判断。
            if($this->admin['sid']  != 0 )
                $sql = $sql." and customId = ".$this->admin['id'];

            $orderNo = $this->findSql($sql);
            $orderNo = $orderNo[0];
            $rows['inOutRecList'] = implode(",",$orderNo);
            //$rows['inOutStockNo'] = $this->admin['id'].date("YmdHis");
            $rows['inOutStyle'] = 1;
            $rows['inOutStockDate'] = date("YmdHis");//date("Y-m-d H-i-s");
            $this->table = 'stockPos';
            $stockNo = $this->find(array('customId'=>$this->admin['id']));
            $rows['stockNo'] =$stockNo;

            $this->table = 'inOutStock';
            $this->create($rows);


        }

        foreach($result as $key=>$val)
        {
            $result[$key]['color'] = $this->color[$val['color']];
            $result[$key]['size'] = $this->size[$val['size']];
        }

        return $result;
    }

    public function lookTotalStock($c,$a)
    {
        $res['inOutStockType'] = G('inOutStockType');
        $res['inOutStockNo'] = G('inOutStockNo');



        if(0 == $res['inOutStockType'])
        {
           $result = $this->preViewTotalStock();
        }else
        {
            $result = $this->postTotalStock();
        }

        include $this->display( $c );

    }


}


